package com.ptszyxx.popose.module.main.home.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.ptszyxx.popose.R;
import com.ptszyxx.popose.common.utils.YActivityUtil;
import com.ysg.base.BaseViewModel;
import com.ysg.binding.command.BindingCommand;
import com.ysg.bus.event.SingleLiveEvent;
import com.ysg.http.BaseListResponse;
import com.ysg.http.HttpUtils;
import com.ysg.http.callback.OnSuccessListResult;
import com.ysg.http.data.CommonRepository;
import com.ysg.http.data.entity.home.HomeSearchEntity;
import com.ysg.utils.YStringUtil;
import com.ysg.utils.YToastUtil;

import java.util.HashMap;
import java.util.Map;

public class HomeSearchVM extends BaseViewModel<CommonRepository> {
    public ObservableField<String> input = new ObservableField<>();

    public HomeSearchVM(@NonNull Application application, CommonRepository model) {
        super(application, model);
    }

    public UIChangeObservable uc = new UIChangeObservable();
    public class UIChangeObservable {
        public SingleLiveEvent onRefreshEvent = new SingleLiveEvent<>();
    }

    public BindingCommand onCloseCommand = new BindingCommand(()-> YActivityUtil.getInstance().close(this));
    public BindingCommand onSearchCommand = new BindingCommand(()-> requestList(true));


    //=========================================接口：start===========================================

    public void requestList(boolean refresh){
        if (refresh){
            page = 1;
        }else{
            page++;
        }
        if (YStringUtil.isEmpty(input.get())){
            YToastUtil.showShort(R.string.input_tip);
            return;
        }


        Map<String, String> map = new HashMap<>();
        map.put("pageno", String.valueOf(page));
        map.put("sousuo", input.get());

        HttpUtils.getInstance()
                .list(model.homeSearchList(map), this,
                        new OnSuccessListResult<HomeSearchEntity>() {
                            @Override
                            public void onSuccessResult(BaseListResponse<HomeSearchEntity> response) {
                                uc.onRefreshEvent.setValue(response.getData());
                            }
                        });
    }
}
